<template>
  <div>
    <div class="demo-image">
      <!-- <el-image
        style="width: 100px; height: 100px"
        :src="this.userinfo.head_img"
        fit="scale-down"
        :preview-src-list="srcList"
      ></el-image>-->
    </div>
    <el-descriptions class="margin-top" title="用户信息" :column="3" border>
      <template slot="extra">
        <el-button type="primary" size="small" @click="dialogFormVisible = true">修改信息</el-button>
      </template>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-user"></i>
          用户名
        </template>
        {{ this.userinfo.user_name }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-mobile-phone"></i>
          手机号
        </template>
        {{ this.userinfo.mobile }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-timer"></i>
          注册时间
        </template>
        {{ this.userinfo.create_at }}
      </el-descriptions-item>
    </el-descriptions>

    <!-- 弹窗 -->
    <el-dialog title="修改用户信息" :visible.sync="dialogFormVisible">
      <el-upload
        action="#"
        list-type="picture-card"
        :auto-upload="true"
        :http-request="uploadHttpRequest"
        :multiple="false"
      >
        <i slot="default" class="el-icon-plus"></i>
        <div slot="file" slot-scope="{file}">
          <img class="el-upload-list__item-thumbnail" :src="file.url" alt />
          <span class="el-upload-list__item-actions">
            <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
              <i class="el-icon-zoom-in"></i>
            </span>
          </span>
        </div>
      </el-upload>
      <!-- 上传图片放大图弹框 -->
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt />
      </el-dialog>

      <el-form :inline="true" :model="userinfo" class="demo-form-inline">
        <el-form-item label="用户名">
          <el-input v-model="userinfo.user_name" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userinfo.mobile" placeholder="手机号" disabled></el-input>
        </el-form-item>
        <el-form-item label="注册时间">
          <el-input v-model="userinfo.create_at" placeholder="注册时间" disabled></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { UserInfo, UpdateUser } from "@/utils/http.js";
export default {
  data() {
    return {
      userinfo: {},
      dialogFormVisible: false,
      fileList: {},
      formInline: {},
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      srcList: [] // 图片大图预览
    };
  },
  methods: {
    userInfo() {
      UserInfo()
        .then(res => {
          if (res.data.code == 200) {
            this.userinfo = res.data.data;
            // console.log(this.userinfo);
            this.$store.commit("setUserData", res.data.data);
            this.srcList.push(res.data.data.head_img);
          } else {
            this.$message({
              message: res.data.message,
              type: "warning",
              center: true
            });
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 更新用户数据
    update() {
      const formData = new FormData();
      formData.append("head", this.fileList);
      // console.log(this.userinfo.user_name);
      let query ={
        username :this.userinfo.user_name
      }
      UpdateUser(formData, query)
        .then(res => {
          if (res.data.code == 200) {
            this.$store.commit("setUserData", res.data);
            this.$message({
              message: "用户数据更新成功",
              type: "success",
              center: true
            });
            this.userInfo();
            this.dialogFormVisible = false;
          } else {
            this.$message({
              message: res.data.message,
              type: "warning",
              center: true
            });
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    uploadHttpRequest(data) {
      this.fileList = data.file;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    }
  },
  mounted() {
    this.userInfo();
  }
};
</script>

<style>
</style>